package turtle.system.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import turtle.system.pt.StatCalc;
/**
 * Trade為一完整的下單交易unit,包含進出時間價錢
 * @author doug
 *
 */
public class Trade {
	public double inPrice;
	public double outPrice;
    public int tradeType = -1;
	public GregorianCalendar inDate;
	public GregorianCalendar outDate;
	public int lot = 0;
	public double getProfit() {
		double profit = 0;
		if (tradeType == Action.LONG_ACTION)
			profit = outPrice - inPrice;
		else if (tradeType == Action.SHORT_ACTION)
			profit = inPrice - outPrice;
		return lot*profit;
	}
	public boolean isPositiveProfit() {
		return getProfit()>0 ? true : false;
	}
	public Trade(int tradeType, double inPrice, GregorianCalendar inDate) {
		this.tradeType = tradeType;
		this.inPrice = inPrice;
		this.inDate = inDate;
		this.lot = 1;
	}
	public Trade(int tradeType, double inPrice, GregorianCalendar inDate, int numLots) {
		this.tradeType = tradeType;
		this.inPrice = inPrice;
		this.inDate = inDate;
		this.lot = numLots;
	}
	public void closeTrade(double outPrice, GregorianCalendar outDate) {
		this.outPrice = outPrice;
		this.outDate = outDate;
	}
	public int daysInHands() {
		long t = outDate.getTime().getTime() - inDate.getTime().getTime();
		Date d = new Date(t);
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(d);
		return c.get(Calendar.DATE);
	}
	/**
	 * We assume the list is sorted by date.
	 * @param list
	 */
	public static void analyzeTradeList(ArrayList<Trade> list) {
		StatCalc all = new StatCalc();
		StatCalc least200 = new StatCalc();
		StatCalc least100 = new StatCalc();
		StatCalc least30 = new StatCalc();

		for (int i=0;i<list.size();i++) {
			Trade t = list.get(i);
			all.enter(t.getProfit());
		}
		System.out.println("全部交易profit:"+printStat(all));
		for (int i=list.size()-200;i<list.size();i++) {
			Trade t = list.get(i);
			least200.enter(t.getProfit());
		}
		System.out.println("最後200筆交易profit:"+printStat(least200));

		for (int i=list.size()-100;i<list.size();i++) {
			Trade t = list.get(i);
			least100.enter(t.getProfit());
		}
		System.out.println("最後100筆交易profit:"+printStat(least100));
		for (int i=list.size()-30;i<list.size();i++) {
			Trade t = list.get(i);
			least30.enter(t.getProfit());
		}		
		System.out.println("最後30筆交易profit:"+printStat(least30));
		//printAllTrade(list);
	}
    private static String printStat(StatCalc stat) {
      StringBuffer buf = new StringBuffer();
      buf.append(stat.getPositiveCount()+"W/"+stat.getNegativeCount()+"L("+(double)stat.getPositiveCount()/(double)stat.getCount()
    		 +"),mean="+(int)stat.getMean()+",max="+(int)stat.getMax()+",min="+(int)stat.getMin());
      return buf.toString();
    }
    public static void printAllTrade(ArrayList<Trade> tradeList) {
    	DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    	DateFormat df1 = new SimpleDateFormat("dd");
    	for (int i=0;i<tradeList.size();i++) {
    		long t = tradeList.get(i).outDate.getTime().getTime() - tradeList.get(i).inDate.getTime().getTime();
    		Date d = new Date(t);
    		
    		System.out.println(df.format(tradeList.get(i).inDate.getTime())+"-"+df.format(tradeList.get(i).outDate.getTime())
    				+","+df1.format(d)
    				+","+tradeList.get(i).getProfit());
    	}
    }
}
